home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Miscellaneous / WBStars2.0 / source / WBStars_main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-24  |  3.8 KB  |  173 lines

  1. /* $VER: WBStars_main.c 2.0b4 (29 May 1997)
  2. */
  3.  
  4. #include <intuition/screens.h>
  5. #include <proto/intuition.h>
  6. #include <dos/dos.h>
  7. #include <proto/exec.h>
  8. #include <proto/dos.h>
  9. #include <proto/intuition.h>
  10. #include <proto/graphics.h>
  11. #include <clib/alib_protos.h>
  12. #include <clib/macros.h>
  13. #include <math.h>
  14. #include <string.h>
  15.  
  16. #include "WBStars_include.h"
  17. #include "WBStars_protos.h"
  18. #include "WBStars_plot.h"
  19.  
  20. struct Screen        *wbench=NULL;
  21. struct RastPort        *RPort;
  22. struct Layer_Info    *LInfo;
  23. struct ColorMap        *cm;
  24.  
  25. long    _stack=4000;
  26. char    *_procname="WBStars";
  27. long    _priority=0;
  28. long    _BackGroundIO=FALSE;
  29.  
  30. int        minx,miny,width,height;
  31. BYTE    CXpri=0;
  32. UBYTE    *hotkey;
  33. char    activated=FALSE;
  34. char    locked=FALSE;
  35. char    version[]="$VER: WBStars 2.0b4 (29 May 1997)\0";
  36. int    bg_color;
  37. int    fg_color;
  38. int    Loop_Wait;
  39. int    Max_Objects;
  40. int    Max_Stick;
  41. int    Check_Stick;
  42. int    V_Flutter;
  43. char    Remember;
  44. char    Force;
  45.  
  46. void    main(long argc, UBYTE **argv)
  47. {
  48.     UBYTE    **ttypes;
  49.     struct EasyStruct    fail1={sizeof(struct EasyStruct),0,"WBStars","failed to open libraries"," Shit ! "};
  50.  
  51.     if( OpenLibraries() )
  52.     {
  53.         /* read the Tooltypes */
  54.         ttypes        = ArgArrayInit(argc, argv);
  55.         CXpri        = (BYTE)ArgInt(ttypes, "CX_PRIORITY", 0);
  56.         hotkey        = ArgString(ttypes, "CX_POPKEY", "control alt w");
  57.         bg_color    = ArgInt(ttypes, "BG_COLOR", -1);
  58.         Loop_Wait    = ArgInt(ttypes, "LOOP_WAIT", 3);
  59.         Max_Objects    = ArgInt(ttypes, "MAX_OBJECTS", 1000);
  60.         Max_Stick    = ArgInt(ttypes, "MAX_STICK", 100);
  61.         V_Flutter    = MAX(MIN(ArgInt(ttypes, "V_FLUTTER", 20),100),0);
  62.         Remember    = (char)(0!=strcmpi(ArgString(ttypes, "REMEMBER", "NO"),"NO\0"));
  63.         Force        = (char)(0!=strcmpi(ArgString(ttypes, "FORCE", "NO"),"NO\0"));
  64.         Check_Stick    = ArgInt(ttypes, "CHECK_STICK", 5);
  65.         SetTaskPri(FindTask(0),MAX(MIN(ArgInt(ttypes, "TOOLPRI", 0),127),-128));
  66.         ArgArrayDone();
  67.  
  68.         if( InitCx() )
  69.         {
  70.             while( HandleCx() )
  71.             {
  72.                 if(activated) PlotObjects();
  73.  
  74.                 Delay(Loop_Wait);
  75.             }
  76.             RemovCx();
  77.         }
  78.     }
  79.     else
  80.     {
  81.         if( IntuitionBase ) EasyRequestArgs(NULL,&fail1,0,NULL);
  82.     }
  83.     CloseLibraries();
  84.     if( activated )    Inactivate();
  85. }
  86.  
  87.  
  88.  
  89.  
  90. void    Inactivate()
  91. {
  92.     if(activated)
  93.     {
  94.         ClearObjects();
  95.         if(OS3)ReleasePen(cm,fg_color);
  96.         UnlockPubScreen( NULL, wbench);
  97.         wbench=NULL;
  98.         activated=FALSE;
  99.     }
  100. }
  101.  
  102. char    Activate()        /* is called, if WBStars should activate */
  103. {                /* returns TRUE if Workbench could be locked */
  104.     short    i;
  105.     short    testcolor[256];
  106.     short    h;
  107.     double    dist;
  108.     double    mindist=-1.0;
  109.     ULONG    color;
  110.     short    ncolors;
  111.     short    r,g,b;
  112.  
  113.     if( activated ) Inactivate();    /* if activated twice inactivate before */
  114.                                         /* locking workbench again */
  115.     if( wbench=LockPubScreen("Workbench") )
  116.     {
  117.         RPort        = &(wbench->RastPort);    /* the WBench´s RPort is the */
  118.         LInfo        = &(wbench->LayerInfo);    /* one we want to draw in */
  119.         minx        = wbench->LeftEdge;
  120.         miny        = wbench->TopEdge + wbench->BarHeight + 1;
  121.         width        = wbench->Width;
  122.         height        = wbench->Height - wbench->BarHeight - 1;
  123.         cm        = wbench->ViewPort.ColorMap;
  124.         ncolors        = (short)(1<<(RPort->BitMap->Depth));
  125.  
  126.         if( bg_color<0 )
  127.         {
  128.             for(i=0;i<(short)(1<<(RPort->BitMap->Depth));i++)
  129.             {
  130.                 testcolor[i]=0;
  131.             }
  132.             for(i=0;i<width;i++)
  133.             {
  134.                 testcolor[ReadPixel(RPort,i+minx,miny)]++;
  135.             }
  136.             bg_color=0;
  137.             h=testcolor[0];
  138.             for(i=1;i<(short)(1<<(RPort->BitMap->Depth));i++)
  139.             {
  140.                 if(testcolor[i]>h)
  141.                 {
  142.                     bg_color=i;
  143.                     h=testcolor[i];
  144.                 }
  145.             }
  146.         }
  147.         if(OS3)
  148.         {
  149.             fg_color=(int)ObtainBestPenA(cm,(ULONG)0xFFFF0000,(ULONG)0xFFFF0000,(ULONG)0xFFFF0000,NULL);
  150.         }
  151.         if(!OS3 || fg_color==-1)
  152.         {
  153.             for(i=0;i<ncolors;i++)
  154.             {
  155.                 color=GetRGB4(cm,(long)i);
  156.                 r=(color>>8)&15;
  157.                 g=(color>>4)&15;
  158.                 b=color&15;
  159.  
  160.                 if( (i!=bg_color) && (((dist=pow((double)(15-r),2.0)+pow((double)(15-g),2.0)+pow((double)(15-b),2.0))<mindist)||(mindist<0.0)) )
  161.                 {
  162.                     mindist=dist;
  163.                     fg_color=i;
  164.                 }
  165.             }
  166.         }
  167.  
  168.         activated=TRUE;
  169.         InitObjects();
  170.     }
  171.  
  172.     return activated;
  173. }